using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using StreetFinder;
using System.Runtime.InteropServices;
using System.Runtime.Serialization.Formatters.Binary;
using System.Net;
using System.IO;
using System.Text;
//using GOL;
using System.Xml.Serialization;
using System.Xml;
//using DataAccess;
using System.Collections.Generic;
//using ViettelClass;
//using ChartRenderStructures;


/// <summary>
/// Summary description for StreetFinderDB
/// </summary>
/// 
namespace StreetFinderDB
{
    public class CStreetFinder
    {
        public string strURL = "http://" + ConfigurationSettings.AppSettings["StreetService"] + ":" + ConfigurationSettings.AppSettings["StreetPort"] + "/StreetFinder?";
        int LocationMaxRadius = 10000;
        int StreetMaxRadius = 1000;

        //Build Get Request
        public string FindGeoInfoString(double dbLon, double dbLat, double dbRadius)
        {
            string strRet = strURL;

            strRet += String.Format("FindType={0}", "GETGEOBLOCKHOUSEINFO");
            strRet += String.Format("&Lon={0}", dbLon);
            strRet += String.Format("&Lat={0}", dbLat);
            strRet += String.Format("&Radius={0}", dbRadius);

            return strRet;

        }

        public string FindDisPt2ObjString(string pt, string coords)
        {
            string strRet = strURL;

            strRet += String.Format("FindType={0}", "DISTANCE_POINT_TO_OBJECTS");
            strRet += String.Format("&CalPts={0}", pt);
            strRet += String.Format("&Coords={0}", coords);

            return strRet;

        }

        public string FindPtInObjString(string pt, string coords, double radius)
        {
            string strRet = strURL;

            strRet += String.Format("FindType={0}", "POINT_IN_OBJECTS");
            strRet += String.Format("&CheckCoords={0}", pt);
            strRet += String.Format("&InCoords={0}", coords);
            strRet += String.Format("&Radius={0}", radius.ToString());

            return strRet;

        }

        public double[] GetLineBetween(string coords, double x1, double y1, double x2, double y2)
        {
            string strQuery = strURL;
            strQuery += String.Format("FindType={0}", "GET_LINE_BETWEEN");
            strQuery += String.Format("&InCoords={0}", coords);
            strQuery += String.Format("&StartLon={0}", x1.ToString());
            strQuery += String.Format("&StartLat={0}", y1.ToString());
            strQuery += String.Format("&EndLon={0}", x2.ToString());
            strQuery += String.Format("&EndLat={0}", y2.ToString());
            return (double[])Find(strQuery);
        }

        public string FindTrafficInfoString(double dbLon, double dbLat, double dbRadius, double dbCrossRoadLimit, int nRoadType, int nRoadLevel)
        {
            string strRet = strURL;

            strRet += String.Format("FindType={0}", "GETTRAFFICINFO");
            strRet += String.Format("&Lon={0}", dbLon);
            strRet += String.Format("&Lat={0}", dbLat);
            strRet += String.Format("&Radius={0}", dbRadius);
            strRet += String.Format("&RadiusCrossRoadLimit={0}", dbCrossRoadLimit);
            strRet += String.Format("&RoadType={0}", nRoadType);
            strRet += String.Format("&RoadLevel={0}", nRoadLevel);

            return strRet;
        }

        public string FindGetTJNInfoString(double dbLon, double dbLat, double dbRadius, int nVehicleType)
        {
            string strRet = strURL;

            strRet += String.Format("FindType={0}", "GETTRAFFICJAMNODES");
            strRet += String.Format("&Lon={0}", dbLon);
            strRet += String.Format("&Lat={0}", dbLat);
            strRet += String.Format("&Radius={0}", dbRadius);
            strRet += String.Format("&VehicleType={0}", nVehicleType);

            return strRet;
        }

        public string FindAddTJNInfoString(string strNodeId)
        {
            string strRet = strURL;

            strRet += String.Format("FindType={0}", "ADDTRAFFICJAMNODE");
            strRet += String.Format("&pNode={0}", strNodeId);

            return strRet;
        }

        public string FindRemoveTJNInfoString(string strNodeId)
        {
            string strRet = strURL;

            strRet += String.Format("FindType={0}", "REMOVETRAFFICJAMNODE");
            strRet += String.Format("&pNode={0}", strNodeId);

            return strRet;
        }

        public string FindAddTJEInfoString(string strEdgeId, string strDist)
        {
            string strRet = strURL;

            strRet += String.Format("FindType={0}", "ADDTRAFFICJAMEDGE");
            strRet += String.Format("&pEdge={0}", strEdgeId);
            strRet += String.Format("&pDist={0}", strDist);

            return strRet;
        }

        public string FindRemoveTJEInfoString(string strEdgeId, string strDist)
        {
            string strRet = strURL;

            strRet += String.Format("FindType={0}", "REMOVETRAFFICJAMEDGE");
            strRet += String.Format("&pEdge={0}", strEdgeId);
            strRet += String.Format("&pDist={0}", strDist);

            return strRet;
        }

        public string FindAddTSInfoString(string strCase, string strTransportType, string strEdge, string strWeight)
        {
            string strRet = strURL;

            strRet += String.Format("FindType={0}", "ADDTRAFFICSTATUS");
            strRet += String.Format("&pCase={0}", strCase);
            strRet += String.Format("&pTransportType={0}", strTransportType);
            strRet += String.Format("&pEdge={0}", strEdge);
            strRet += String.Format("&pWeight={0}", strWeight);

            return strRet;
        }

        public string FindRemoveTSInfoString(string strCase, string strTransportType, string strEdge, string strWeight)
        {
            string strRet = strURL;

            strRet += String.Format("FindType={0}", "REMOVETRAFFICSTATUS");
            strRet += String.Format("&pCase={0}", strCase);
            strRet += String.Format("&pTransportType={0}", strTransportType);
            strRet += String.Format("&pEdge={0}", strEdge);
            strRet += String.Format("&pWeight={0}", strWeight);

            return strRet;
        }

        public string FindTSPString(string strPlaces, VehicleTypes eVehicleType)
        {
            string strQuery = strURL;

            strQuery += "FindType=TSP";
            strQuery += "&Places=" + strPlaces;
            strQuery += "&VehicleType=" + ((int)eVehicleType).ToString();

            return strQuery;
        }
        public string FindShortestPathString(string strPlaces, int nFindType, VehicleTypes eVehicleType, int nTargets, double dbRadius, int iFindMode, int iInstructionMode)
        {
            string strQuery = strURL;

            strQuery += "FindType=ShortestPath";
            strQuery += "&Places=" + strPlaces;
            strQuery += "&FindStreetType=" + nFindType.ToString();
            strQuery += "&VehicleType=" + ((int)eVehicleType).ToString();
            strQuery += "&MaxTargets=" + nTargets.ToString();
            strQuery += "&MaxDistance=" + dbRadius.ToString();
            strQuery += "&FindMode=" + iFindMode.ToString();
            strQuery += "&InstructionMode=" + iInstructionMode.ToString();

            return strQuery;
        }
        public string CreateQuery_GetContour(int iZoomLevel, double dbLongitude, double dbLatitude, int iType)
        {
            string strRet = strURL;

            strRet += String.Format("Action={0}", "GetContour");
            strRet += String.Format("&ZoomLevel={0}", iZoomLevel);
            strRet += String.Format("&Longitude={0}", dbLongitude);
            strRet += String.Format("&Latitude={0}", dbLatitude);
            strRet += String.Format("&Type={0}", iType);

            return strRet;
        }
        public FindStreetResult FindMultiShortestPath(string strPlaces, int nFindType, VehicleTypes eVehicleType, int iFindMode, string iArrInstruction, int Alley)
        {
            string strQuery = strURL;

            strQuery += "FindType=METHODMULTISHORTESTPATH";
            strQuery += "&Places=" + strPlaces;
            strQuery += "&FindStreetType=" + nFindType.ToString();
            strQuery += "&VehicleType=" + ((int)eVehicleType).ToString();
            strQuery += "&FindMode=" + iFindMode.ToString();
            strQuery += "&Instruction=" + iArrInstruction;
            strQuery += "&Alley=" + Alley.ToString();

            return (FindStreetResult)Find(strQuery);
        }
        public FindStreetResult FindShortestPathBlockHouse(string strPlaces, int nFindType, VehicleTypes eVehicleType, int iFindMode, string iArrInstruction, int Alley)
        {
            string strQuery = strURL;

            strQuery += "FindType=SHORTESTPATHBLOCKHOUSE";
            strQuery += "&Places=" + strPlaces;
            strQuery += "&FindStreetType=" + nFindType.ToString();
            strQuery += "&VehicleType=" + ((int)eVehicleType).ToString();
            strQuery += "&FindMode=" + iFindMode.ToString();
            strQuery += "&Instruction=" + iArrInstruction;
            strQuery += "&Alley=" + Alley.ToString();

            return (FindStreetResult)Find(strQuery);
        }
        public GeoReturnAddressInfo GetGeoInfo(double dbX, double dbY, double dbRadius)
        {
            string strQuery = strURL;

            strQuery += "FindType=GET_GEO_INFO";
            strQuery += "&Places=" + dbX + "," + dbY;
            strQuery += "&Radius=" + dbRadius.ToString();

            return (GeoReturnAddressInfo)Find(strQuery);
        }


        public string FindSequentialString(string strPlaces, VehicleTypes eVehicleType, bool bAutoClose, int iFindMode, int iInstructionMode)
        {
            int nFindStreetType = (int)FindStreetTypes.Sequential;
            if (bAutoClose)
                nFindStreetType |= (int)FindStreetTypes.Closed;

            string strQuery = strURL;

            strQuery += "FindType=ShortestPath";
            strQuery += "&Places=" + strPlaces;
            strQuery += "&FindStreetType=" + nFindStreetType.ToString();
            strQuery += "&VehicleType=" + ((int)eVehicleType).ToString();
            strQuery += "&FindMode=" + ((int)iFindMode).ToString();
            strQuery += "&InstructionMode=" + ((int)iInstructionMode).ToString();
            return strQuery;

            return strQuery;
        }
        public string FindOneToManyString(string strPlaces, VehicleTypes eVehicleType, bool bInversed, int iFindMode, int iInstructionMode)
        {
            int nFindStreetType = (int)FindStreetTypes.OneToMany;
            if (bInversed)
                nFindStreetType |= (int)FindStreetTypes.Inversed;

            string strQuery = strURL;
            char[] splitter = new char[1];
            splitter[0] = ',';
            string[] strCoords = strPlaces.Split(splitter);

            strQuery += "FindType=ShortestPath";
            strQuery += "&Places=" + strPlaces;
            strQuery += "&FindStreetType=" + nFindStreetType.ToString();
            strQuery += "&VehicleType=" + ((int)eVehicleType).ToString();
            strQuery += "&MaxTargets=" + ((strCoords.Length / 2) - 1).ToString();
            strQuery += "&FindMode=" + iFindMode.ToString();
            strQuery += "&InstructionMode=" + iInstructionMode.ToString();

            return strQuery;

        }
        public string SearchObjectsString(double x, double y, string strLayer, string strObjType, double dbMaxDistance, int iPage, int iPageSize, double nMaxObjects, VehicleTypes eVehicleType, bool bInversed)
        {
            int nFindStreetType = 0;
            if (bInversed)
                nFindStreetType |= (int)FindStreetTypes.Inversed;

            string strQuery = strURL;
            string strPlaces = String.Format("{0},{1}", x, y);

            strQuery += "FindType=SearchObjects";
            strQuery += "&Places=" + strPlaces;
            strQuery += "&FindStreetType=" + nFindStreetType.ToString();
            strQuery += "&VehicleType=" + ((int)eVehicleType).ToString();
            strQuery += "&MaxTargets=" + nMaxObjects.ToString();
            strQuery += "&MaxDistance=" + dbMaxDistance.ToString();
            strQuery += "&Layer= NeighbourObject";
            strQuery += "&ObjType=" + strObjType;

            return strQuery;
        }
        public string SearchREObjectsString(double x, double y, string strLayer, string strObjType, double dbMaxDistance, int iPage, int iPageSize, bool bInversed)
        {
            //int nFindStreetType = 0;
            //if (bInversed)
            //    nFindStreetType |= (int)FindStreetTypes.Inversed;
            UnicodeConverter.Converter converter = new UnicodeConverter.Converter();
            string strQuery = strURL;
            string strPlaces = String.Format("{0},{1}", x, y);

            dbMaxDistance = ((dbMaxDistance > StreetMaxRadius) ? StreetMaxRadius : dbMaxDistance);

            strQuery += "FindType=SearchObjects";
            strQuery += "&Places=" + strPlaces;
            strQuery += "&MaxDistance=" + dbMaxDistance.ToString();
            strQuery += "&Layer=RealEstate";
            strQuery += "&PageSize=" + iPageSize;
            strQuery += "&Page=" + iPage;
            strQuery += "&ObjType=" + converter.EncodeUnicode(strObjType);

            return strQuery;
        }
        public string SearchObjectsNearByPoint(double x, double y, string strLayer, string strObjType, double dbMaxDistance, int iPage, int iPageSize, bool bInversed)
        {
            //int nFindStreetType = 0;
            //if (bInversed)
            //    nFindStreetType |= (int)FindStreetTypes.Inversed;
            UnicodeConverter.Converter converter = new UnicodeConverter.Converter();
            string strQuery = strURL;
            string strPlaces = String.Format("{0},{1}", x, y);

            dbMaxDistance = ((dbMaxDistance > LocationMaxRadius) ? LocationMaxRadius : dbMaxDistance);

            strQuery += "FindType=SearchObjects";
            strQuery += "&Places=" + strPlaces;
            strQuery += "&MaxDistance=" + dbMaxDistance.ToString();
            strQuery += "&Layer=RealEstate";
            strQuery += "&PageSize=" + iPageSize;
            strQuery += "&Page=" + iPage;
            strQuery += "&ObjType=" + converter.EncodeUnicode(strObjType);

            return strQuery;
        }
        public string FindObjectLocationString(int Id, string strLayer)
        {
            string strQuery = strURL;

            strQuery += "FindType=FindObjLocation";
            strQuery += "&ObjID=" + Id.ToString();
            strQuery += "&Layer=" + strLayer;

            return strQuery;
        }
        public string FindThematicString(double dLeft, double dTop, double dRight, double dBottom, int iLevel)
        {
            string strQuery = "http://" + ConfigurationSettings.AppSettings["ServiceHost"] + ":510/Business?Action=Clipping";
            strQuery += "&Left=" + dLeft;
            strQuery += "&Top=" + dTop;
            strQuery += "&Right=" + dRight;
            strQuery += "&Bottom=" + dBottom;
            strQuery += "&Level=" + iLevel;

            return strQuery;
        }
        public object FindPolylineNearby(string strPlaces, string strLayer, string strObjType, double dbMaxDistance, int iPage, int iPageSize, double nMaxObjects, VehicleTypes eVehicleType, bool bInversed)
        {
            int nFindStreetType = 0;
            if (bInversed)
                nFindStreetType |= (int)FindStreetTypes.Inversed;

            StringBuilder strQuery = new StringBuilder("FindType=SearchObjects");
            strQuery.AppendFormat("&Places={0}", strPlaces);
            strQuery.AppendFormat("&FindStreetType={0}", nFindStreetType);
            strQuery.AppendFormat("&VehicleType={0}", ((int)eVehicleType).ToString());
            strQuery.AppendFormat("&MaxTargets={0}", nMaxObjects.ToString());
            strQuery.AppendFormat("&MaxDistance={0}", dbMaxDistance.ToString());
            strQuery.AppendFormat("&Layer=NeighbourPolyline");
            strQuery.AppendFormat("&ObjType={0}", strObjType);
            strQuery.AppendFormat("&PageSize={0}", iPageSize);
            strQuery.AppendFormat("&Page={0}", strPlaces);

            //return (SearchObjectsResult2)Find(strQuery);
            byte[] buff = Encoding.ASCII.GetBytes(strQuery.ToString());


            return start_post(buff, strURL);

        }
        public string FindRealEstateString(double dLeft, double dTop, double dRight, double dBottom, int iLevel, string sCondition)
        {
            string strQuery = "http://" + ConfigurationSettings.AppSettings["ServiceHost"] + ":510/Business?Action=RealEstate";
            strQuery += "&Left=" + dLeft;
            strQuery += "&Top=" + dTop;
            strQuery += "&Right=" + dRight;
            strQuery += "&Bottom=" + dBottom;
            strQuery += "&Level=" + iLevel;
            strQuery += "&Condition=" + sCondition;

            return strQuery;
        }
        public string FromLatlngToInfoString(string strLng, string strLat, int nFlag, double dbDistance)
        {
            string strQuery = strURL;

            strQuery += "FindType=FROMLATLNGTOINFO";
            strQuery += "&Places=" + strLng + "," + strLat;
            strQuery += "&Radius=" + dbDistance.ToString();
            strQuery += "&Flag=" + nFlag.ToString();
            return strQuery;
        }
        public string FromLatlngToStreetString(double strLng, double strLat, double dbDistance)
        {
            string strQuery = strURL;

            strQuery += "FindType=FROMLATLNGTOSTREET";
            strQuery += "&Places=" + strLng.ToString() + "," + strLat.ToString();
            strQuery += "&Radius=" + dbDistance.ToString();
            return strQuery;
        }
        public string UpdateRatingLevelString(string Guid, int nRating, int nDisplayLevel)
        {
            string strQuery = strURL;

            strQuery += "FindType=UPDATE_RATING_LEVEL_INDEX";
            strQuery += "&Guid=" + Guid;
            strQuery += "&nRating=" + nRating.ToString();
            strQuery += "&nDisplayLevel=" + nDisplayLevel.ToString();
            return strQuery;
        }

       /* public ClipEliminatePathByIdResult ClipEliminatePathId(double[] arrPlaces, double dbLeft, double dbTop, double dbRight, double dbBottom, double dbEVal)
        {
            ObjectClipPathSending objRequest = new ObjectClipPathSending();

            objRequest.dbCoor = arrPlaces;
            objRequest.dbLeft = dbLeft;
            objRequest.dbTop = dbTop;
            objRequest.dbRight = dbRight;
            objRequest.dbBottom = dbBottom;
            objRequest.dbEVal = dbEVal;
            objRequest.iClip = -1;

            byte[] buff = DataSerializer.SerializeBinary(objRequest);

            return ((ClipEliminatePathByIdResult)start_post(buff, strURL + "FindType=CLIP_ELIMINATE_PATH_BY_ID"));
        }*/

        public string GetTrafficPtOnCrossRoad(double x1, double y1, double x2, double y2, double dbDistance)
        {
            string strQuery = strURL;
            strQuery += "FindType=GET_TRAFFIC_POINT_ON_CROSSROAD";
            strQuery += "&StartLon=" + x1.ToString();
            strQuery += "&StartLat=" + y1.ToString();
            strQuery += "&EndLon=" + x2.ToString();
            strQuery += "&EndLat=" + y2.ToString();
            strQuery += "&Distance=" + dbDistance.ToString();
            return (string)Find(strQuery);
        }

        public string GetTrafficPtOnCrossRoad(string x1, string y1, string x2, string y2, double dbDistance)
        {
            string strQuery = strURL;
            strQuery += "FindType=GET_TRAFFIC_POINT_ON_CROSSROAD";
            strQuery += "&StartLon=" + x1;
            strQuery += "&StartLat=" + y1;
            strQuery += "&EndLon=" + x2;
            strQuery += "&EndLat=" + y2;
            strQuery += "&Distance=" + dbDistance.ToString();
            return (string)Find(strQuery);
        }

      /*  public TrafficEdgesByPoint GetTrafficEdgesByPt(string x, string y)
        {
            string strQuery = strURL;
            strQuery += "FindType=GET_TRAFFIC_EDGES_BY_POINT";
            strQuery += "&Lon=" + x;
            strQuery += "&Lat=" + y;
            return (TrafficEdgesByPoint)Find(strQuery);
        }

        public TrafficEdgesByPoint GetTrafficEdgesByPtEx(string x, string y, string strName)
        {
            string strQuery = strURL;
            strQuery += "FindType=GET_TRAFFIC_EDGES_BY_POINT_EX";
            strQuery += "&Lon=" + x;
            strQuery += "&Lat=" + y;
            strQuery += "&Name=" + strName;
            return (TrafficEdgesByPoint)Find(strQuery);
        }

        public TrafficEdgesByPoint GetTrafficCrossroadByTwoPt(double StartLon, double StartLat, double EndLon, double EndLat)
        {
            string strQuery = strURL;
            strQuery += "FindType=GET_TRAFFIC_CROSSROAD_BY_TWOPOINT";
            strQuery += "&StartLon=" + StartLon.ToString();
            strQuery += "&StartLat=" + StartLat.ToString();
            strQuery += "&EndLon=" + EndLon.ToString();
            strQuery += "&EndLat=" + EndLat.ToString();
            return (TrafficEdgesByPoint)Find(strQuery);
        }

        public TrafficEdgesByPoint GetTrafficCrossroadByTwoPt(string StartLon, string StartLat, string EndLon, string EndLat)
        {
            string strQuery = strURL;
            strQuery += "FindType=GET_TRAFFIC_CROSSROAD_BY_TWOPOINT";
            strQuery += "&StartLon=" + StartLon;
            strQuery += "&StartLat=" + StartLat;
            strQuery += "&EndLon=" + EndLon;
            strQuery += "&EndLat=" + EndLat;
            return (TrafficEdgesByPoint)Find(strQuery);
        }
        */
        public double[] GetPointByDistance(double StartLon, double StartLat, double EndLon, double EndLat, double PLon, double PLat, string streetName, double distance)
        {
            string strQuery = strURL;
            strQuery += "FindType=POINT_BY_DISTANCE";
            strQuery += "&Coords=" + StartLon.ToString() + "," + StartLat.ToString() + "," + EndLon.ToString() + "," + EndLat.ToString();
            strQuery += "&Lon=" + PLon.ToString();
            strQuery += "&Lat=" + PLat.ToString();
            strQuery += "&StreetName=" + streetName;
            strQuery += "&Distance=" + distance.ToString();
            return (double[])Find(strQuery);
        }
        public string[] GetForeCastNextCrossRoads(double StartLon, double StartLat, double EndLon, double EndLat, double distance)
        {
            string strQuery = strURL;
            strQuery += "FindType=FORECAST_NEXT_CROSSROADS";
            strQuery += "&StartLon=" + StartLon.ToString();
            strQuery += "&StartLat=" + StartLat.ToString();
            strQuery += "&EndLon=" + EndLon.ToString();
            strQuery += "&EndLat=" + EndLat.ToString();
            strQuery += "&Radius=" + distance.ToString();
            return (string[])Find(strQuery);
        }
        public bool GetDynamicRoadDirectionLaw(string strPlaces, string streetName, int TransoprtType, int DirectionType, int Mode)
        {
            string strQuery = strURL;
            strQuery += "FindType=DYNAMIC_ROAD_DIRECTION_LAW";
            strQuery += "&Coords=" + strPlaces;
            strQuery += "&StreetName=" + streetName;
            strQuery += "&TransoprtType=" + TransoprtType.ToString();
            strQuery += "&DirectionType=" + DirectionType.ToString();
            strQuery += "&Mode=" + Mode.ToString();
            return (bool)Find(strQuery);
        }
        public bool GetDynamicRoadForbiddenLaw(string strPlaces, string FromName, string ToName, int TransoprtType, int DirectionType, double Radius, int Mode)
        {
            string strQuery = strURL;
            strQuery += "FindType=DYNAMIC_ROAD_FORBIDDEN_LAW";
            strQuery += "&Coords=" + strPlaces;
            strQuery += "&FromRoadName=" + FromName;
            strQuery += "&ToRoadName=" + ToName;
            strQuery += "&TransoprtType=" + TransoprtType.ToString();
            strQuery += "&DirectionType=" + DirectionType.ToString();
            strQuery += "&Radius=" + Radius.ToString();
            strQuery += "&Mode=" + Mode.ToString();
            return (bool)Find(strQuery);
        }

        //Category By Admin
        public DataSet GetLocationInAdminByCategories(string arrCoor, string AdminIDs, string AdminTypes, string CatIDs, string CatTypes, int Page, int PageSize)
        {
            string strQuery = strURL;
            strQuery += "FindType=GET_LOCATION_IN_ADMIN_BY_CATEGORY";
            strQuery += "&Places=" + arrCoor;
            strQuery += "&AdminIDs=" + AdminIDs;
            strQuery += "&AdminTypes=" + AdminTypes;
            strQuery += "&CatIDs=" + CatIDs;
            strQuery += "&CatTypes=" + CatTypes;
            strQuery += "&Page=" + Page.ToString();
            strQuery += "&PageSize=" + PageSize.ToString();

            return (DataSet)Find(strQuery);
        }
        
        //Build Post Request
        public double[][] ProcessClipEliminatePath(double[] arrPlaces, double dbLeft, double dbTop, double dbRight, double dbBottom, double dbEVal)
        {
            ObjectClipPathSending objRequest = new ObjectClipPathSending();

            objRequest.dbCoor = arrPlaces;
            objRequest.dbLeft = dbLeft;
            objRequest.dbTop = dbTop;
            objRequest.dbRight = dbRight;
            objRequest.dbBottom = dbBottom;
            objRequest.dbEVal = dbEVal;
            objRequest.iClip = -1;

            byte[] buff = DataSerializer.SerializeBinary(objRequest);

            return ((double[][])start_post(buff, strURL + "FindType=CLIPPATH"));
        }
        public double[][] ProcessClipEliminatePolygon(double[] arrPlaces, double dbLeft, double dbTop, double dbRight, double dbBottom, double dbEVal, int iClip)
        {
            ObjectClipPathSending objRequest = new ObjectClipPathSending();

            objRequest.dbCoor = arrPlaces;
            objRequest.dbLeft = dbLeft;
            objRequest.dbTop = dbTop;
            objRequest.dbRight = dbRight;
            objRequest.dbBottom = dbBottom;
            objRequest.dbEVal = dbEVal;
            objRequest.iClip = iClip;

            byte[] buff = DataSerializer.SerializeBinary(objRequest);

            return ((double[][])start_post(buff, strURL + "FindType=CLIPPATH"));
        }
        public ObjectGetInterHouse ProcessGetInterHouse(int[] arrPlaces, int iInputNo, int iGraphicID)
        {
            ObjectRequest objRequest = new ObjectRequest();

            objRequest.ObjectInput = arrPlaces;
            objRequest.iInputNo = iInputNo;
            objRequest.iGraphicID = iGraphicID;

            byte[] buff = DataSerializer.SerializeBinary(objRequest);

            return ((ObjectGetInterHouse)start_post(buff, strURL + "FindType=GET_INTER_HOUSE"));
        }
        public object LuceneShortestPathNearby(double[] dbPoints, int nFindType, VehicleTypes eVehicleType, double dbRadius, int iPage, int iPageSize, string strKeyWord)
        {

            /*string strQuery = strURL;

            strQuery += "FindType=SHORTESTPATHAPI_NEARBY";
            strQuery += "&Places=" + strPlaces;
            strQuery += "&FindStreetType=" + nFindType.ToString();
            strQuery += "&VehicleType=" + ((int)eVehicleType).ToString();            
            strQuery += "&MaxDistance=" + dbRadius.ToString();
            strQuery += "&PageSize=" + iPageSize;
            strQuery += "&Page=" + iPage;
            strQuery += "&ObjType=" + strKeyWord;

            return (SearchObjectsResult2)Find(strQuery);

            StringBuilder strQuery = new StringBuilder("");
            strQuery.AppendFormat("&Places={0}", strPlaces);
            strQuery.AppendFormat("&FindStreetType={0}", nFindType);
            strQuery.AppendFormat("&VehicleType={0}", ((int)eVehicleType).ToString());
            strQuery.AppendFormat("&MaxDistance={0}", dbRadius);
            strQuery.AppendFormat("&PageSize={0}", iPageSize);
            strQuery.AppendFormat("&Page={0}", iPage);
            strQuery.AppendFormat("&ObjType={0}", strKeyWord);*/

            ObjectStreetRequestSending obj = new ObjectStreetRequestSending();
            obj.dbRadius = ((dbRadius > StreetMaxRadius) ? StreetMaxRadius : dbRadius);
            obj.eVehicleType = (int)eVehicleType;
            obj.iPage = iPage;
            obj.iPageSize = iPageSize;
            obj.nFindType = nFindType;
            obj.strKeyWord = strKeyWord;
            obj.dbPlaces = dbPoints;

            byte[] buff = DataSerializer.SerializeBinary(obj);

            //return (SearchObjectsResult2)Find(strQuery);
            //byte[] buff = Encoding.ASCII.GetBytes(strQuery.ToString());

            return start_post(buff, strURL + "FindType=SHORTESTPATHAPI_NEARBY");

        }

        //Comunication functions
        public object Find(string strQuery)
        {
            object result = null;

            try
            {

                string strURL = (string)strQuery;
                WebRequest req = WebRequest.Create(strURL);
                //req.Timeout = 15000;

                WebResponse resp = req.GetResponse();


                Stream stream = resp.GetResponseStream();

                long len = resp.ContentLength;

                if (len > 0)
                {
                    byte[] data = new byte[len];
                    int nRead = 0;

                    while (nRead < len)
                    {
                        nRead += stream.Read(data, nRead, (int)len - nRead);
                    }

                    result = DataSerializer.DeSerializeBinary(data);
                }

                stream.Close();
                resp.Close();
            }
            catch (Exception ex)
            {
                string strError = ex.Message;
                return null;
            }
            return result;
        }
        public object RequestPostXML(object obj, string sURL)
        {
            byte[] buff = new UTF8Encoding().GetBytes(DataSerializer.SerializeXML(obj));

            return start_post(buff, sURL);
        }
        private object start_post(byte[] buffer, string sURL)
        {
            try
            {

                //Initialisation, we use localhost, change if appliable
                HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(sURL);
                //Our method is post, otherwise the buffer (postvars) would be useless
                WebReq.Method = "POST";
                //We use form contentType, for the postvars.
                WebReq.ContentType = "application/x-www-form-urlencoded";
                //The length of the buffer (postvars) is used as contentlength.
                WebReq.ContentLength = buffer.Length;
                WebReq.Timeout = 3600000;// 1 hour
                //We open a stream for writing the postvars
                Stream PostData = WebReq.GetRequestStream();
                //Now we write, and afterwards, we close. Closing is always important!
                PostData.Write(buffer, 0, buffer.Length);
                PostData.Close();
                //Get the response handle, we have no true response yet!
                HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();

                Stream stream = WebResp.GetResponseStream();
                object result = null;
                long len = WebResp.ContentLength;

                if (len > 0)
                {
                    byte[] data = new byte[len];
                    int nRead = 0;

                    while (nRead < len)
                    {
                        nRead += stream.Read(data, nRead, (int)len - nRead);
                    }

                    result = DataSerializer.DeSerializeBinary(data);
                }

                stream.Close();
                WebResp.Close();
                return result;
            }
            catch(Exception ex )
            {
                return null;
            }
        }
      /*  public ArrayLocationObj GetLocationInBound(string sCoor, string sCategory, int nPage)
        {
            ArrayLocationObj arrlistObjResult = null;
            //LocationInBoundInfo info = null;
            DataSet info = null;
            List<LocationStructure> listObjResult = null;
            double dbRadius = 1000;
            try
            {
                bool bFound = false;
                info = SearchLocationInBound(sCoor, sCategory, nPage, dbRadius);
                if (info != null)
                {
                    listObjResult = new List<LocationStructure>();
                    arrlistObjResult = new ArrayLocationObj();
                    arrlistObjResult.ItemFound = int.Parse(info.Tables[1].Rows[0]["HitSize"].ToString());
                    int len = info.Tables[0].Rows.Count;
                    for (int i = 0; i < len; i++)
                    {
                        LocationStructure ls = new LocationStructure();
                        ls.Name = info.Tables[0].Rows[i]["Name"].ToString();
                        ls.FullAddress = info.Tables[0].Rows[i]["Address"].ToString();
                        ls.Phone = info.Tables[0].Rows[i]["Tel"].ToString();
                        ls.LocationId = info.Tables[0].Rows[i]["GuId"].ToString();
                        ls.Latitude = double.Parse(info.Tables[0].Rows[i]["Latitude"].ToString());
                        ls.Longitude = double.Parse(info.Tables[0].Rows[i]["Longitude"].ToString());
                        ls.VietbandoId = info.Tables[0].Rows[i]["VietbandoID"].ToString();
                        ls.Name = ls.Name.Replace("<B>", "");
                        ls.Name = ls.Name.Replace("</B>", "");
                        ls.Name = ls.Name.Replace("<b>", "");
                        ls.Name = ls.Name.Replace("</b>", "");
                        //ls.Category = sCategory;
                        listObjResult.Add(ls);
                    }
                    arrlistObjResult.ArrayLocation = listObjResult;
                }
                else
                {
                    return null;
                }
                return arrlistObjResult;
            }
            catch (Exception e)
            {
                return null;
            }
        }
        */
        /// <summary>
        /// Phuong
        /*public List<MarkerStruct> GetLocationInBound(string sCoor, string sCategory, int nPage, int xIndex, int yIndex, int level)
        {
            DataSet info = null;
            List<MarkerStruct> aMarker = null;
            double dbRadius = 1000;
            try
            {
                info = SearchLocationInBound(sCoor, sCategory, nPage, dbRadius);
                int dx = -xIndex * 256;
                int dy = -yIndex * 256;
                if (info.Tables.Count > 0 && info.Tables[0].Rows.Count > 0)
                {
                    aMarker = new List<MarkerStruct>();
                    int len = info.Tables[0].Rows.Count;
                    int x = 0;
                    int y = 0;
                    for (int i = 0; i < len; i++)
                    {
                        x = 0;
                        y = 0;

                        double Latitude = double.Parse(info.Tables[0].Rows[i]["Latitude"].ToString());
                        double Longitude = double.Parse(info.Tables[0].Rows[i]["Longitude"].ToString());
                        string name = info.Tables[0].Rows[i]["Name"].ToString();
                        string category = info.Tables[0].Rows[i]["CategoryCode"].ToString();
                        TileSystem.LatLongToPixelXY(Latitude, Longitude, level, out x, out y);

                        x += dx;
                        y += dy;

                        string id = info.Tables[0].Rows[i]["GuId"].ToString();

                        MarkerStruct item = new MarkerStruct(Latitude, Longitude, x, y, id, category, name);
                        aMarker.Add(item);
                    }

                    return aMarker;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }*/
        /*
        public List<MarkerStruct> GetLocationInBound2(string sCoor, string sCategory, int nPage, int dx, int dy, int level)
        {
            DataSet info = null;
            List<MarkerStruct> aMarker = null;
            double dbRadius = 1000;
            try
            {
                info = SearchLocationInBound(sCoor, sCategory, nPage, dbRadius);
                if (info != null)
                {
                    aMarker = new List<MarkerStruct>();
                    int len = info.Tables[0].Rows.Count;
                    int x = 0;
                    int y = 0;
                    for (int i = 0; i < len; i++)
                    {
                        x = 0;
                        y = 0;

                        double Latitude = double.Parse(info.Tables[0].Rows[i]["Latitude"].ToString());
                        double Longitude = double.Parse(info.Tables[0].Rows[i]["Longitude"].ToString());

                        TileSystem.LatLongToPixelXY(Latitude, Longitude, level, out x, out y);

                        x += dx;
                        y += dy;

                        string id = info.Tables[0].Rows[i]["GuId"].ToString();

                        MarkerStruct item = new MarkerStruct(x, y, id);
                        aMarker.Add(item);
                    }

                    return aMarker;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception e)
            {
                return null;
            }
        }
        */
       /* public DataSet SearchLocationInBound(string arrCoor, string arrCategory, int nPage, double radius)
        {
            try
            {
                Search objSearch = new Search();
                DataSet result = objSearch.SearchLocationInBound(arrCoor, arrCategory, nPage, radius);

                return result;
            }
            catch
            {
                return null;
            }

        }*/
        public bool UpdateRatingLevelInfo(string Guid, int nRating, int nDisplayLevel)
        {
            string strQuery = UpdateRatingLevelString(Guid, nRating, nDisplayLevel);
            bool bRet = false;
            bRet = (bool)Find(strQuery);
            return bRet;
        }
    }

}